<%@ taglib uri="http://bhf.org/taglibs/bhs" prefix="bhs" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>BHF JSON Service Tests</title>
</head>
<body>
    <script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/yahoo/yahoo-min.js"></script>
    <script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/json/json-min.js"></script>
    <script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/event/event-min.js"></script>
    <script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/connection/connection-min.js"></script>

    <script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/yahoo-dom-event/yahoo-dom-event.js"></script>
    <script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/logger/logger-min.js"></script>
    <script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/yuitest/yuitest.js"></script>

    <script type="text/javascript" src="bhf-services.js"></script>
    
    <script type="text/javascript">
        var anEcho = <bhs:service service="test.EchoService" absolute="true" />;
        var Assert = YAHOO.util.Assert;

        //
        // Pass One: make the calls
        //
        YAHOO.tool.TestRunner.add( new YAHOO.tool.TestCase({
            name: "Test Service Invocations",

            testInlineAsynchronous: function()
            {
                var expected    = "hello";
                var result      = anEcho.echo( { value: expected, delay: 0 } );
                
                Assert.areSame( expected, result );
            },

            testInlineVoid: function()
            {
                var expected    = "hello";
                var result      = anEcho.voidReturn( { value: expected, delay: 0 } );

                Assert.isUndefined( result );
            },

            testInlineException: function()
            {
                try
                {
                    anEcho.echoException();
                    Assert.isTrue( false, "Unexpected success" );
                }
                catch( e )
                {
                    Assert.areSame( "ConversionError", e.message );
                    Assert.areSame( "Exception", e.name );
                }
            },

            testSimpleInvocation: function()
            {
                var expected = "hello";
                var that = this;

                anEcho.echo(
                    {
                        success: function( o ) { that.resume( function() { Assert.areSame( expected, o ); } ); },
                        failure: function( e ) { that.resume( function() { Assert.fail( e ); } ); }
                    },
                    { value: expected, delay: 0 }
                );

                this.wait( function()
                    {
                        Assert.fail( "Timed out" );
                    }, 5000
                );
            },

            testDefaultCallback: function()
            {
                var expected = "hello";
                var that = this;

                anEcho.setDefaultCallback(
                    {
                        success: function( o ) { that.resume( function() { Assert.areSame( expected, o ); } ); }
                    }
                );
                anEcho.echo(
                    {
                        failure: function( e ) { that.resume( function() { Assert.fail( e ); } ); }
                    },
                    { value: expected, delay: 0 }
                );

                this.wait( function()
                    {
                        Assert.fail( "Timed out" );
                    }, 5000
                );
            },

            testDefaultCallbackOverride: function()
            {
                var expected = "hello";
                var that = this;

                anEcho.setDefaultCallback(
                    {
                        success: function( o ) { that.resume( function() { Assert.fail( "no override" ); } ) },
                        failure: function( e ) { that.resume( function() { Assert.fail( e ); } ) }
                    }
                );
                anEcho.echo(
                    {
                        success: function( o ) { that.resume( function() { Assert.areSame( expected, o ); } ); }
                    },
                    { value: expected, delay: 0 }
                );

                this.wait( function()
                    {
                        Assert.fail( "Timed out" );
                    }, 5000
                );
            },

            testAbortInvocation: function()
            {
                var expected    = "hello";
                var that        = this;
                var c;

                c = anEcho.echo(
                    {
                        success: function() { that.resume( function() { Assert.fail( "not aborted" ); } ) },
                        failure: function( e ) { that.resume( function() { Assert.fail( e ); } ) }
                    },
                    { value: expected, delay: 0 }
                );
                BHF.util.Connect.abort( c.conn );

                this.wait( function()
                    {
                    }, 1000
                );
            },

            testVoidInvocation: function()
            {
                var expected = undefined;
                var that = this;

                anEcho.voidReturn(
                    {
                        success: function( o ) { that.resume( function() { Assert.areSame( expected, o ); } ); },
                        failure: function( e ) { that.resume( function() { Assert.fail( e ); } ); }
                    },
                    { value: expected, delay: 0 }
                );

                this.wait( function()
                    {
                        Assert.fail( "Timed out" );
                    }, 5000
                );
            },

            testSimpleException: function()
            {
                var that = this;

                anEcho.echoException(
                    {
                        success: function( o ) { that.resume( function() { Assert.fail( "Unexpected success" ); } ); },
                        failure: function( e ) { that.resume( function() {
                            Assert.isObject( e );
                            Assert.areSame( "ConversionError", e.message );
                            Assert.areSame( "Exception", e.name );
                        } ); }
                    }
                );

                this.wait( function()
                    {
                        Assert.fail( "Timed out" );
                    }, 5000
                );
            },

            testPessimisticLocking1: function()
            {
                var that = this;
                var x = 0;

                x = 0;
                anEcho.echo(
                    {
                        success: function( o ) { that.resume( function() { Assert.fail( "success should not have been called" ); } ); },
                        failure: function( e ) { that.resume( function() { Assert.fail( "failure should not have been called" ); } ); },
                        precondition: BHF.Service.PRECONDITIONS.SAME_ARG,
                        rollback: function( o ) { that.resume( function() { } ); }
                    },
                    function() { return { value: x, delay: 500 }; }
                );
                x = 2;

                this.wait( function()
                    {
                        Assert.fail( "Timed out" );
                    }, 5000
                );
            },

            testPessimisticLocking2: function()
            {
                var that = this;
                var x = 0;

                x = 0;
                anEcho.echo(
                    {
                        success: function( o ) { that.resume( function() { } ); },
                        failure: function( e ) { that.resume( function() { Assert.fail( "failure should not have been called" ); } ); },
                        precondition: BHF.Service.PRECONDITIONS.SAME_ARG,
                        rollback: function( o ) { that.resume( function() { Assert.fail( "rollback should not have been called" ); } ); }
                    },
                    function() { return { value: x, delay: 500 }; }
                );

                this.wait( function()
                    {
                        Assert.fail( "Timed out" );
                    }, 5000
                );
            },

            testPessimisticLockingRetry: function()
            {
                var that = this;
                var x = 0;

                x = 0;
                anEcho.echo(
                    {
                        success: function() { that.resume(); },
                        failure: function() { that.resume( function() { Assert.fail( "failure should not have been called" ); } ); },
                        precondition: BHF.Service.PRECONDITIONS.SAME_ARG,
                        rollback: BHF.Service.ROLLBACK_POLICY.RETRY
                    },
                    function() { return { value: x, delay: 500 }; }
                );
                x = 2; // Causes a failure, then retry

                this.wait( function()
                    {
                        Assert.fail( "Timed out" );
                    }, 5000
                );
            },

            testPessimisticLockingIgnore: function()
            {
                var that = this;
                var x = 0;
                anEcho.echo(
                    {
                        success: function() { that.resume( function() { Assert.fail( "success should not have been called" ); } ); },
                        failure: function() { that.resume( function() { Assert.fail( "failure should not have been called" ); } ); },
                        precondition: BHF.Service.PRECONDITIONS.SAME_ARG,
                        rollback: function( txn ) { BHF.Service.ROLLBACK_POLICY.IGNORE( txn ); that.resume(); }
                    },
                    function() { return { value: x, delay: 500 }; }
                );
                x = 2; // Causes a failure, then retry

                this.wait( function()
                    {
                        Assert.fail( "Timed out" );
                    }, 5000
                );
            }
        }));

        YAHOO.tool.TestManager.load();
        
    </script>
</body>
</html>